<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\admin\model\Users;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("users")->where('createtime', 'between time', [$starttime, $endtime])
            ->field('createtime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = db('order')->where(['order_status'=>['neq',0],'addtime'=>strtotime(date("Y-m-d"))])->count();//自提订单数
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $dtime = strtotime(date("Y-m-d"));
        $money1 = db('order')->where(['order_status'=>['neq',0],'addtime'=>['>=',strtotime(date("Y-m-d"))]])->sum('money');
        $money2 =db('splicingorder')->where(['order_status'=>['not in',['0','3']],'addtime'=>['>=',strtotime(date("Y-m-d"))]])->sum('money');
        $money7 = db('order')->where(['order_status'=>['neq',0],'addtime'=>['>=',strtotime("-7 days")]])->sum('money');
        $money71 =db('splicingorder')->where(['order_status'=>['not in',['0','3']],'addtime'=>['>=',strtotime("-7 days")]])->sum('money');
        $money30 = db('order')->where(['order_status'=>['neq',0],'addtime'=>['>=',strtotime(date("Y-m"))]])->sum('money');
        $money301 =db('splicingorder')->where(['order_status'=>['not in',['0','3']],'addtime'=>['>=',strtotime(date("Y-m"))]])->sum('money');

        $this->view->assign([
            'totaluser'         => Users::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => Users::where(['createtime'=>['>=',$dtime]])->count(),   //今日注册,
            'todayuserlogin'    => $money1+$money2,
            'sevendau'          => $money7+$money71,
            'thirtydau'         => $money30+$money301,
            'threednu'          => Users::whereTime('createtime', '-3 days')->count(),//3日新增
            'sevendnu'          => Users::whereTime('createtime', '-7 days')->count(),//7日新增
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => db('splicingorder')->where(['order_status'=>['not in',['0','3']],'addtime'=>strtotime(date("Y-m-d"))])->count(),//拼单订单数
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}
